<template>
  <div>
    <h2>会员管理</h2>
    <button @click="add">新增</button>
    <ul>
      <li v-for="item in list" :key="item.id">
       {{item.username}} ---  {{item.nickname}}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data () {
    return {
      list: []
    }
  },
  created () {
    this.query()
  },
  methods: {
    query () {
      this.$http.get('user/list').then(res => {
        if (res.err > 0) {
          console.error(res.desc)
        } else {
          this.list = res.result
        }
      })
    },
    add () {
      const username = prompt('请输入账号！')
      const nickname = prompt('请输入昵称！')

      if (username && nickname) {
        this.$http.post('user/add', JSON.stringify({ username, nickname })).then(res => {
          if (res.err > 0) {
            console.error(res.desc)
          } else {
            // 添加成功后刷新
            this.query()
          }
        })
      }
    }
  }
}
</script>
